package com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.entity.TYcodeMasterDataStandardDetail;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.entity.TYcodeMasterDataStandardDetailEnum;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.service.ITYcodeMasterDataStandardDetailEnumService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.dto.TYcodeMasterDataStandardDetailListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.mapper.TYcodeMasterDataStandardDetailMapper;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.service.ITYcodeMasterDataStandardDetailService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author dongxinhang
 * @since 2024-07-17
 */
@Service
@AllArgsConstructor
public class TYcodeMasterDataStandardDetailServiceImpl extends BaseService<TYcodeMasterDataStandardDetailMapper, TYcodeMasterDataStandardDetail> implements ITYcodeMasterDataStandardDetailService {

	private ITYcodeMasterDataStandardDetailEnumService tYcodeMasterDataStandardDetailEnumService;

	@Override
	public IPage<TYcodeMasterDataStandardDetail> getPageList(TYcodeMasterDataStandardDetailListDto pageListDto) {
		Wrapper<TYcodeMasterDataStandardDetail> wrapper = Wrappers.<TYcodeMasterDataStandardDetail>query().lambda()
				.eq(!StringUtil.isEmpty(pageListDto.getStandard_id()), TYcodeMasterDataStandardDetail::getStandardId, pageListDto.getStandard_id())
				.orderByAsc(TYcodeMasterDataStandardDetail::getOrderNum);
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public boolean addTYcodeMasterDataStandardDetail(TYcodeMasterDataStandardDetail tYcodeMasterDataStandardDetail, List<TYcodeMasterDataStandardDetailEnum> tYcodeMasterDataStandardDetailEnumList) {
		String tYcodeMasterDataStandardDetailId = IdWorker.get32UUID();
		tYcodeMasterDataStandardDetail.setId(tYcodeMasterDataStandardDetailId);
		tYcodeMasterDataStandardDetailEnumList.forEach(tYcodeMasterDataStandardDetailEnum -> tYcodeMasterDataStandardDetailEnum.setDetailId(tYcodeMasterDataStandardDetailId));
		tYcodeMasterDataStandardDetailEnumService.saveBatch(tYcodeMasterDataStandardDetailEnumList);
		boolean isSuccess = this.save(tYcodeMasterDataStandardDetail);
		return isSuccess;
	}

	@Override
	public boolean updateTYcodeMasterDataStandardDetail(String id, TYcodeMasterDataStandardDetail tYcodeMasterDataStandardDetail, List<TYcodeMasterDataStandardDetailEnum> tYcodeMasterDataStandardDetailEnumList) {
		tYcodeMasterDataStandardDetail.setId(id);
		// TYcodeMasterDataStandardDetailEnum
		List<String> tYcodeMasterDataStandardDetailEnumIdList = new ArrayList<>();
		for (TYcodeMasterDataStandardDetailEnum tYcodeMasterDataStandardDetailEnum : tYcodeMasterDataStandardDetailEnumList) {
			String tYcodeMasterDataStandardDetailEnumId = tYcodeMasterDataStandardDetailEnum.getId();
			if (StringUtil.isEmpty(tYcodeMasterDataStandardDetailEnumId)) {
				tYcodeMasterDataStandardDetailEnum.setDetailId(id);
			} else {
				tYcodeMasterDataStandardDetailEnumIdList.add(tYcodeMasterDataStandardDetailEnumId);
			}
		}
		if (CollectionUtil.isNotEmpty(tYcodeMasterDataStandardDetailEnumIdList)) {
			tYcodeMasterDataStandardDetailEnumService.remove(Wrappers.<TYcodeMasterDataStandardDetailEnum>query().lambda()
					.notIn(TYcodeMasterDataStandardDetailEnum::getId, tYcodeMasterDataStandardDetailEnumIdList)
					.eq(TYcodeMasterDataStandardDetailEnum::getDetailId, id));
		}
		tYcodeMasterDataStandardDetailEnumService.saveOrUpdateBatch(tYcodeMasterDataStandardDetailEnumList);
		return this.updateById(tYcodeMasterDataStandardDetail);
	}

	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			tYcodeMasterDataStandardDetailEnumService.remove(Wrappers.<TYcodeMasterDataStandardDetailEnum>query().lambda().in(TYcodeMasterDataStandardDetailEnum::getDetailId, ids));
			return true;
		}
		return false;
	}

	public List<TYcodeMasterDataStandardDetailEnum> getTYcodeMasterDataStandardDetailEnumByParentId(String parentId){
		Wrapper wrapper = Wrappers.<TYcodeMasterDataStandardDetailEnum>query().lambda().eq(TYcodeMasterDataStandardDetailEnum::getDetailId, parentId);
		return tYcodeMasterDataStandardDetailEnumService.list(wrapper);
	}

	public List<TYcodeMasterDataStandardDetail> getDetailListBystandardId(String standard_id){
		Wrapper wrapper = Wrappers.<TYcodeMasterDataStandardDetail>query().lambda().eq(TYcodeMasterDataStandardDetail::getStandardId, standard_id).orderByAsc(TYcodeMasterDataStandardDetail::getOrderNum);
		return this.list(wrapper);
	}
}